<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/5
 * Time: 13:59
 * 捷克部门，大客户组
 */

namespace app\modules\api\controllers;

use app\lib\BaseFunction;
use app\modules\api\models\Quote;
use app\modules\api\service\StaffService;
use yii;
use app\modules\api\service\SellService;


class SellController extends BaseController
{
    public function actionList()
    {
        $p = is_numeric(yii::$app->request->get('p')) ? yii::$app->request->get('p') : 1;
        $s = is_numeric(yii::$app->request->get('s')) ? yii::$app->request->get('s') : 15;
        $where = [];
        $where['product_num'] = trim(yii::$app->request->get('product_num'));
        $where['service'] = trim(yii::$app->request->get('staff'));
        $where['leader'] = yii::$app->request->get('leader');
        $where['product_name'] = trim(yii::$app->request->get('product_name'));
        $where['product_name_en'] = trim(yii::$app->request->get('product_name_en'));
        $where['product_status'] = trim(yii::$app->request->get('product_status'));
        $where['begin_time'] = strtotime(yii::$app->request->get('begin_time'));
        $where['end_time'] = strtotime(yii::$app->request->get('end_time'));

        $service = new SellService();
        $list = $service->quoteList($p, $s, $where);
        foreach ($list as $k => $v) {
            $tmp = json_decode($v['img'], true);
            $list[$k]['img'] = $tmp[0]['url'];
            $list[$k]['imgs'] = json_decode($v['imgs'], true);
            $tmp2 = "";
            if ($v['supplier']) {
                $tmp2 = json_decode($v['supplier'], true);
                $list[$k]['supplier'] = (is_array($tmp2) && (!empty($tmp2))) ? $tmp2[$v['main_supplier']] : '';
            } else {
                $list[$k]['supplier'] = '';

            }

        }
        echo json_encode($list);
    }

    public function actionSize()
    {
        $where = [];
        $where['product_num'] = trim(yii::$app->request->get('product_num'));
        $where['service'] = trim(yii::$app->request->get('staff'));
        $where['leader'] = yii::$app->request->get('leader');
        $where['product_name'] = trim(yii::$app->request->get('product_name'));
        $where['product_name_en'] = trim(yii::$app->request->get('product_name_en'));
        $where['product_status'] = trim(yii::$app->request->get('product_status'));
        $where['begin_time'] = strtotime(yii::$app->request->get('begin_time'));
        $where['end_time'] = strtotime(yii::$app->request->get('end_time'));
        if ($this->user['role'] != 1 && $this->user['role'] != 2 && $this->user['role'] != 10) {
            $where['service'] = $this->user['job_number'];
        }
        $service = new SellService();
        $size = $service->quoteSize($where);
        echo json_encode(['code' => 1, 'num' => $size]);
    }

    public function actionSuppliersAndLeader()
    {
        $data = [];
        $service = new SellService();
        $data['leader'] = $service->leader();
        $data['supplier'] = $service->getSupplier();
        $data['type'] = $service->getQuoteType();
        $service = new StaffService();
        $data['staff'] = $service->staff();
        echo json_encode(['code' => 1, 'msg' => $data]);
    }

    public function actionAdd()
    {

    }

    /**
     * 捷克大客户组联系人
     */
    public function actionCzekhLeaders()
    {
        $service = new SellService();
        $list = $service->leader();
        echo json_encode($list);
    }

    public function actionAddLeaders()
    {
        if (yii::$app->request->isPost) {
            $data = [];
            $data['name'] = yii::$app->request->post('name');
            $data['remark'] = yii::$app->request->post('remark');
            $service = new SellService();
            $re = $service->addLeader($data);
            if ($re) {
                echo json_encode(['code' => 1, 'msg' => '添加成功']);
            } else {
                echo json_encode(['code' => 1, 'msg' => '添加失败，请重试']);
            }

        }
    }

    public function actionSupplier()
    {
        $service = new SellService();
        $list = $service->getSupplier();
        echo json_encode(['code' => 1, 'list' => $list]);
    }

    public function actionAddSupplier()
    {
        if (yii::$app->request->isPost) {
            $data = [];
            $data['company_name'] = yii::$app->request->post('company_name');
            $data['name'] = yii::$app->request->post('name');
            $data['phone'] = yii::$app->request->post('phone');
            $data['qq'] = yii::$app->request->post('qq');
            $data['remark'] = yii::$app->request->post('remark');
            if ($data['company_name'] && $data['name']) {
                $service = new SellService();
                $re = $service->addSupplier($data);
                if ($re) {
                    echo json_encode(['code' => 1, 'msg' => '添加成功']);
                } else {
                    echo json_encode(['code' => 0, 'msg' => '添加失败,请重试']);
                }
            }
        }
    }

    /**
     * @return bool
     */
    public function actionAddQuote()
    {
        if (yii::$app->request->isPost) {
            $data = file_get_contents("php://input");
            $data = json_decode($data, true);
            if (!isset($data['product_num'])) {
                return false;
            }
            if (!isset($data['product_name_en'])) {
                return false;
            }
            if (!isset($data['product_name'])) {
                return false;
            }
            if (!isset($data['ship_fee_unit'])) {
                return false;
            }
            if (isset($data['img'])) {
                $data['img'] = json_encode($data['img']);
            }
            if (isset($data['imgs'])) {
                $data['imgs'] = json_encode($data['imgs']);
            }
            $data['service'] = $this->user['job_number'];
            $data['update_at'] = time();
            $service = new SellService();
            $re = $service->addQuote($data);
            if ($re) {
                echo json_encode(['code' => 1, 'msg' => '添加成功']);
            } else {
                echo json_encode(['code' => 0, 'msg' => '添加失败，请重试']);
            }
        }
    }

    /**
     * 根据参数修改大客户报价
     */
    public function actionEditQuote()
    {
        $id = yii::$app->request->post('id');
        $model = new Quote();
        $re = $model->checkEdit($id);
        if (!$re) {
            echo json_encode(['code' => 0, 'msg' => '当前记录已被锁定,无法修改']);
            exit;
        }
        if (is_numeric($id)) {
            $param = yii::$app->request->post('param');
            $value = yii::$app->request->post('value');
            $data = [];
            $data['update_at'] = time();
            $data[$param] = $value;
            $re = yii::$app->db->createCommand()
                ->update(yii::$app->db->tablePrefix . 'quote2', $data, ['id' => $id])
                ->execute();
            if ($re) {
                echo json_encode(['code' => 1, 'msg' => '修改成功']);
            } else {
                echo json_encode(['code' => 0, 'msg' => '修改失败，请重试']);
            }
        }
    }

    public function actionEditQuoteAll()
    {
        $data = file_get_contents('php://input');
        $data = json_decode($data, true);
        $id = $data['id'];
        $model = new Quote();
        $re = $model->checkEdit($id);
        if (!$re) {
            echo json_encode(['code' => 0, 'msg' => '当前记录已被锁定,无法修改']);
            exit;
        }
        unset ($data['id']);
        $data['img'] = json_encode($data['img']);
        $data['imgs'] = json_encode($data['imgs']);
        $data['update_at'] = time();
        $re = yii::$app->db->createCommand()
            ->update(yii::$app->db->tablePrefix . 'quote2', $data, ['id' => $id])
            ->execute();
        if ($re) {
            echo json_encode(['code' => 1, 'msg' => '修改成功']);
        } else {
            echo json_encode(['code' => 0, 'msg' => '修改失败，请重试']);
        }
    }

    public function actionDelQuote()
    {
        if ($id = yii::$app->request->post('id')) {
            BaseFunction::check_edit_auth($this->user['role']);
            $service = new SellService();
            $re = $service->delQuote($id);
            if ($re) {
                echo json_encode(['code' => 1, 'msg' => '删除成功']);
            } else {
                echo json_encode(['code' => 0, 'msg' => '删除失败']);
            }
        }
    }

    public function actionCustomer()
    {
        $id = yii::$app->request->get('id');
        if (is_numeric($id)) {
            $service = new SellService();
            $info = $service->getQuoteById($id);
            $info['img'] = json_decode($info['img']);
            $info['imgs'] = json_decode($info['imgs']);
            echo json_encode($info);
        }
    }

    public function actionUploadQuoteExcel()
    {
        $file = $_FILES['file'];
        $re = BaseFunction::check_excel($file['type']);
        if (!$re) {
            return json_encode(['code' => 0, 'msg' => '不是excel文件']);
        }
        $path = ROOT_PATH . (BaseFunction::upload($file));
        $suffix = explode('.', $file['name']);
        $excel = BaseFunction::get_excel($path, $suffix[count($suffix) - 1]);
        $sheet = $excel->getSheet(0);
        $highestRow = $sheet->getHighestRow();
        $data = [];
        for ($i = 2; $i <= $highestRow; $i++) {
            $product = $excel->getActiveSheet()->getCell('A' . $i)->getValue();//产品编号
            if ($product) {
                $data[$i][] = $product;
                $data[$i][] = $excel->getActiveSheet()->getCell('B' . $i)->getValue();//英文名称
                $data[$i][] = $excel->getActiveSheet()->getCell('C' . $i)->getValue();//中文名
                $data[$i][] = $excel->getActiveSheet()->getCell('D' . $i)->getValue();//样品状态
                $data[$i][] = $excel->getActiveSheet()->getCell('E' . $i)->getValue();//大货状态
                $data[$i][] = $excel->getActiveSheet()->getCell('F' . $i)->getValue();//类型
                $data[$i][] = $excel->getActiveSheet()->getCell('G' . $i)->getValue();//负责人
                $data[$i][] = $excel->getActiveSheet()->getCell('H' . $i)->getValue();//材料
                $data[$i][] = $excel->getActiveSheet()->getCell('I' . $i)->getValue();//捷克负责人
                $data[$i][] = $excel->getActiveSheet()->getCell('J' . $i)->getValue();//客户链接
                $data[$i][] = $excel->getActiveSheet()->getCell('K' . $i)->getValue();//客户要求
                $data[$i][] = $excel->getActiveSheet()->getCell('L' . $i)->getValue();//采购量1
                $data[$i][] = $excel->getActiveSheet()->getCell('M' . $i)->getValue();//采购量2
                $data[$i][] = $excel->getActiveSheet()->getCell('N' . $i)->getValue();//采购量3
                $data[$i][] = $excel->getActiveSheet()->getCell('O' . $i)->getValue();//出厂价1
                $data[$i][] = $excel->getActiveSheet()->getCell('P' . $i)->getValue();//加价利润1
                $data[$i][] = $excel->getActiveSheet()->getCell('Q' . $i)->getValue();//出厂价2
                $data[$i][] = $excel->getActiveSheet()->getCell('R' . $i)->getValue();//加价利润2
                $data[$i][] = $excel->getActiveSheet()->getCell('S' . $i)->getValue();//出厂价3
                $data[$i][] = $excel->getActiveSheet()->getCell('T' . $i)->getValue();//加价利润3
                $data[$i][] = $excel->getActiveSheet()->getCell('U' . $i)->getValue();//运费单价
                $data[$i][] = $excel->getActiveSheet()->getCell('V' . $i)->getValue();//装箱
                $data[$i][] = $excel->getActiveSheet()->getCell('W' . $i)->getValue();//长
                $data[$i][] = $excel->getActiveSheet()->getCell('X' . $i)->getValue();//宽
                $data[$i][] = $excel->getActiveSheet()->getCell('Y' . $i)->getValue();//高
                $data[$i][] = $excel->getActiveSheet()->getCell('Z' . $i)->getValue();//毛重
                $data[$i][] = $excel->getActiveSheet()->getCell('AA' . $i)->getValue();//供应商1
                $data[$i][] = $excel->getActiveSheet()->getCell('AB' . $i)->getValue();//供应商2
                $data[$i][] = $excel->getActiveSheet()->getCell('AC' . $i)->getValue();//供应商3
            }
        }
        $keys = [
            'product_num', 'product_name_en', 'product_name', 'sample', 'product_status', 'type', 'service', 'material', 'leader', 'links',
            'customer_requirements', 'amount1', 'amount2', 'amount3', 'factory_price1', 'add_profit1',
            'factory_price2', 'add_profit2', 'factory_price3', 'add_profit3', 'ship_fee_unit', 'box',
            'box_length', 'box_width', 'box_height', 'weight', 'supplier1', 'supplier2', 'supplier3'
        ];

        $re = yii::$app->db->createCommand()->batchInsert(yii::$app->db->tablePrefix . 'quote2', $keys, $data)->execute();
        if ($re) {
            echo json_encode(['code' => 1, 'msg' => '添加成功']);
        } else {
            echo json_encode(['code' => 0, 'msg' => '添加失败']);
        }
    }

    public function actionLockRecord()
    {
        $id = yii::$app->request->post('id');
        $status = yii::$app->request->post('status');
        if (is_numeric($id)) {
            BaseFunction::check_edit_auth($this->user['role']);
            $model = new Quote();
            $re = $model->lockRecord($id, $status);
            if ($re) {
                echo json_encode(['code' => 1, 'msg' => '更新成功']);
            } else {
                echo json_encode(['code' => 0, 'msg' => '更新失败,请重试']);
            }
        }
    }

}